<template>
  <div class="wrapper"
       ref="wrapper">
    <div class="content">
      <slot></slot>
    </div>
  </div>
</template>

<script>
// 引入better-scroll下拉滚动组件 进行包装
import BetterScroll from 'better-scroll'
export default {
  name: 'Scroll',
  props: {
    probeType: {
      type: Number,
      default: 0
    },
    pullUpLoad: {
      type: Boolean,
      default: false
    },
    data: {
      type: Array,
      default: () => {
        return []
      }
    },
  },
  data () {
    return {
      scroll: null
    }
  },
  mounted () {
    // 1、创建srcoll对象
    this.scroll = new BetterScroll(this.$refs.wrapper, {
      click: true,
      mouseWheel: true,//pc鼠标滚轮
      probeType: this.probeType,
      pullUpLoad: this.pullUpLoad//true
    })

    // 2、监听滚动的位置
    this.scroll.on('scroll', (position) => {
      // console.log(position);
      // 谁想用传输给谁
      this.$emit('scroll', position)
    })

    //3、监听上拉事件滚动到底部
    if (this.probeType === 2 || this.probeType === 3) {
      this.scroll.on('pullingUp', () => {
        //监听滚动到底部
        // console.log('监听滚动到底');
        this.$emit('pullingUp')
      })
      // console.log(this.scroll);
    }

  },

  components: {
  },
  methods: {
    scrollTo (x, y, time = 300) {
      this.scroll && this.scroll.scrollTo(x, y, time)
    },
    refresh () {
      this.scroll && this.scroll.refresh()
    },
    finishPullUp () {
      this.scroll.finishPullUp()
    },
    getScrollY () {
      return this.scroll ? this.scroll.y : 0
    }
  },
}
</script>
<style scoped>
</style>